/* ======================================================
* 
* file:		file_monitor.h
* brief:	
* author:	80070525(chenjian)
* version:	1.0.0
* date:		2015-03-05 15:44:04
* 
* ======================================================*/
#ifndef FILE_MONITOR_H
#define FILE_MONITOR_H
#include <map>
#include <string>
#include <functional>
#include <thread>
#include <mutex>

typedef std::function<void ()> FileHandle;

class FileMonitor
{
public:
	FileMonitor(bool detach = true);

	int addWatch(const std::string& file, const FileHandle& handle);
	void delWatch(const std::string& file);
	void start();
	void stop();

private:
	void init();
	void monitor();

	bool getHandle(const std::string& file, FileHandle& handle);
	bool getHandle(int watchfd, FileHandle& handle);
	
private:
	bool								is_detach_;
	int									notify_fd_;
	std::thread*						thread_;
	std::mutex							mutex_;
	std::map<int, std::string>			fd2Files_;
	std::map<std::string, FileHandle>	file2Handles_;

};
#endif
